package com.bingfabao.renwubianpai;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/2/25  14:10
 * 描述:
 */
public class TestRenWuBianPai {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //
       /* CompletableFuture<Void> completableFuture=CompletableFuture.supplyAsync(()->{

            System.out.println("======1==>"+(1+1));

            return "2";
        }).thenAccept(yyy->{

            System.out.println( yyy+"--->"+"888888");
        });*/


        //任务的合并  合并前边的任务的值
        /*CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("========1");
            return "--1--";
        });
        for (int i=0;i<5;i++){

            if(i!=4){
                stringCompletableFuture = stringCompletableFuture.thenCombine(CompletableFuture.supplyAsync(() -> {
                    return "--2--";
                }), (x, y) -> {
                    System.out.println("========2");
                    return x + y;
                });
            }else{
                stringCompletableFuture.thenAccept(yy->{
                    System.out.println("+++++++++++yy-->"+yy);
                });
            }
        }*/


        //所有的任务中最先完成任务的一个
        CompletableFuture<String> t1 = CompletableFuture.supplyAsync(() -> {

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "OK1";
        });

        CompletableFuture<String> t2 = CompletableFuture.supplyAsync(() -> {

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "OK2";
        });

        CompletableFuture<String> t3 = CompletableFuture.supplyAsync(() -> {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "OK3";
        });

        CompletableFuture<Object> objectCompletableFuture = CompletableFuture.anyOf(t1, t2, t3);
        System.out.println(objectCompletableFuture.get());

    }



}
